package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spit")
public class SpitController {
    @Autowired
    private SpitService spitService;

    /**
     * 1.查询所有的吐槽信息列表
     * @return  吐槽信息和状态信息
     */
    @GetMapping
    public Result findAll(){
        try {
            List<Spit> spitList = spitService.findAll();
            return new Result(StatusCode.OK,true,"查询所有吐槽信息成功!",spitList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询所有吐槽信息失败!");
        }
    }

    /**
     * 2.查询一个吐槽信息
     * @param spitId    吐槽信息id
     * @return  吐槽信息和状态信息
     */
    @GetMapping("{spitId}")
    public Result findOne(@PathVariable String spitId){
        try {
            Spit spitList = spitService.findOne(spitId);
            return new Result(StatusCode.OK,true,"查询一个吐槽信息成功!",spitList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询一个吐槽信息失败!");
        }
    }

    /**
     * 3.添加吐槽信息
     * @param spit  要添加的吐槽信息
     * @return 是否添加成功的状态信息
     */
    @PostMapping
    public Result add(@RequestBody Spit spit){
        try {
            spitService.add(spit);
            return new Result(StatusCode.OK,true,"添加吐槽信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"添加吐槽信息失败！");
        }
    }

    /**
     * 4.修改吐槽信息
     * @param spitId 要修改的吐槽列表id
     * @param spit  要修改的吐槽信息
     * @return 是否修改成功的状态信息
     */
    @PutMapping("{spitId}")
    public Result update(@PathVariable String spitId,@RequestBody Spit spit){
        try {
            spit.set_id(spitId);
            spitService.update(spit);
            return new Result(StatusCode.OK,true,"修改吐槽信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"修改吐槽信息失败！");
        }
    }

    /**
     * 5.删除吐槽信息
     * @param spitId    要删除的吐槽信息id
     * @return  是否删除的状态信息
     */
    @DeleteMapping("{spitId}")
    public Result delete(@PathVariable String spitId){
        try {
            spitService.delete(spitId);
            return new Result(StatusCode.OK,true,"删除吐槽信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"删除吐槽信息失败！");
        }
    }

    /**
     * 6.根据上级ID查询吐槽数据（分页）
     * @param parentid  要查询的上级id
     * @param page  当前页
     * @param size  页的大小
     * @return  页信息和状态信息
     */
    @GetMapping("comment/{parentid}/{page}/{size}")
    public Result comment(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
        try {
            PageResult<Spit> spitPage = spitService.comment(parentid,page,size);
            return new Result(StatusCode.OK,true,"根据上级ID查询吐槽数据（分页）成功！",spitPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"根据上级ID查询吐槽数据（分页）失败！");
        }
    }

    /**
     * 7.点赞方法
     * @param spitId    要点赞的吐槽信息id
     * @return  是否点赞成功的状态信息
     */
    @PutMapping("thumbup/{spitId}")
    public Result thumbup(@PathVariable String spitId){
        try {
            boolean b = spitService.thumbup(spitId);
            if (b){
                return new Result(StatusCode.OK,true,"点赞成功！");

            }
            return new Result(StatusCode.ERROR,false,"不能重复点赞！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"点赞失败！");
        }
    }
}
